package com.supermarket.shop.system.dao.impl;

import com.supermarket.shop.system.dao.VipDao;
import com.supermarket.shop.system.domain.Vip;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

public class VipDaoImpl implements VipDao {

    //从系统文件中读取所有的商品信息
    @Override
    public ArrayList<Vip> BufferedReaderFile() throws IOException {
        //创建接收VIP会员信息的集合
        ArrayList<Vip> list = new ArrayList<Vip>();
        //创建文件
        File file=new File("D:\\下载\\file\\超市商品购物系统\\vip.txt");
        file.createNewFile();
        //创建字符缓冲输入流
        BufferedReader br = new BufferedReader(new
                FileReader("D:\\下载\\file\\超市商品购物系统\\vip.txt"));
        String[] split;
        String line = null;
        //循环遍历接收所有的商品信息
        while ((line = br.readLine()) != null) {
            split = line.split(",");
            //接收每一个商品信息的对象信息
            Vip vip= Vip.getVip(split);
            list.add(vip);
        }
        //关闭流
        br.close();
        //如果商品信息没有就返回空集合否则返回list集合
        if (list.size()==0) {
            return null;
        }
        return list;
    }

    //写入系统中文件的商品信息
    @Override
    public void BufferedWriterFile(Vip vip) throws IOException {
        SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd");
        BufferedWriter bw = new BufferedWriter(new
                FileWriter("D:\\下载\\file\\超市商品购物系统\\vip.txt", true));
        bw.write(vip.getId() + ",");
        bw.write(vip.getName() + ",");
        bw.write(vip.getPhone() + ",");
        bw.write(vip.getPass() + ",");
        bw.write(sdf.format(vip.getDate())+ ",");
        bw.newLine();
        bw.flush();
        bw.close();
    }


    //判断name的VIP会员存在不
    public boolean isExist(String name) throws IOException {
        ArrayList<Vip> list=this.BufferedReaderFile();
        if(list==null){
            return false;
        }
        for (Vip vip:list) {
            if(vip!=null&&vip.getName().equals(name)){
                return true;
            }
        }
        return false;
    }

    //找出VIP会员中id最大值
    @Override
    public Integer maxCommodityId() throws IOException {
        ArrayList<Vip> list=this.BufferedReaderFile();
        Integer max=0;
        if(list==null){
            return max;
        }
        for(Vip vip:list){
            if(vip!=null&&max<vip.getId()){
                max=vip.getId();
            }
        }
        return max;
    }

    //此方法的功能是增加一个VIP会员用户
    @Override
    public boolean addVip(Vip vip) throws IOException {
        this.BufferedWriterFile(vip);
        return true;
    }

    //此方法的功能是查找全部的VIP会员用户
    @Override
    public void findAllVip() throws IOException {
        ArrayList<Vip> list=BufferedReaderFile();
        if(list==null){
            System.out.println("VIP序号"+"\t\t\t"+"VIP名称"+"\t\t\t"+"电话号码"+"\t\t\t"+"VIP密码"+"\t\t\t"+"注册日期");
            System.out.println(" - - - - - - 目前还没有VIP会员用户 - - - - - - ");
            return;
        }else{
            System.out.println("VIP序号"+"\t\t\t"+"VIP名称"+"\t\t\t"+"电话号码"+"\t\t\t"+"VIP密码"+"\t\t\t"+"注册日期");
            for(Vip vip:list){
                vip.show();
            }
        }
    }

    //此方法删除指定姓名的VIP会员用户
    @Override
    public void deleteVipByName(String name) throws IOException {
        ArrayList<Vip> list=BufferedReaderFile();
        if(list==null){
            System.out.println("你查找的文件里没有要找的信息!");
        }
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i)!=null&&list.get(i).getName().equals(name)){
                list.remove(i);
                System.out.println("已经移除了!!");
                i--;
            }
        }
        this.deleteFile(list);
    }

    //重新设置VIP会员用户的所有信息
    @Override
    public void updateVipByname(Vip vip, String nameupdate) throws IOException {
        ArrayList<Vip> list=BufferedReaderFile();
        if(list==null){
            System.out.println("你查找的文件里没有要找的信息!");
        }
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i)!=null&&list.get(i).getName().equals(vip.getName())){
                list.set(i,vip);
                list.get(i).setName(nameupdate);
            }
        }
        deleteFile(list);

    }


    //删除文件,创建同名新文件,并上传list集合的商品信息
    public void deleteFile(ArrayList<Vip> list) throws IOException {
        File file = new File("D:\\下载\\file\\超市商品购物系统\\vip.txt");
        while (true) {
            if (file.exists() && file.isFile()) {
                file.delete();
            } else {
                break;
            }
        }
        for (Vip vip : list) {
            this.BufferedWriterFile(vip);
        }
    }


}
